package de.dopichaj.labrador.index.lucene;


import java.io.File;
import java.io.IOException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import de.dopichaj.labrador.analysis.PorterStemAnalyzer;
import de.dopichaj.labrador.index.FileSystemIndex;
import de.dopichaj.labrador.index.IndexVersionException;
import de.dopichaj.labrador.index.WritableIndex;
import de.dopichaj.labrador.index.XMLIndexWriter;
import de.dopichaj.labrador.search.NEXISearcher;
import de.dopichaj.labrador.search.erg.ElementRelationshipGraph;
import de.dopichaj.labrador.search.lucene.LuceneNEXISearcher;
import de.dopichaj.labrador.util.Configuration;


public final class LuceneFileSystemIndex extends FileSystemIndex {

    private XMLIndexWriter writer;
    private final LuceneIndex luceneIndex;
    private final ElementRelationshipGraph erg;
    protected static final String luceneName = "lucene";

    public LuceneFileSystemIndex(File indexDirectory, String[] stopWords, Configuration configuration) throws IOException {
        
        super(indexDirectory, stopWords, configuration);
        final File luceneDirectory = new File(indexDirectory, luceneName);
        luceneDirectory.mkdir();
        this.luceneIndex = makeLuceneIndex(luceneDirectory);
        this.erg = new ElementRelationshipGraph();
    }

    private LuceneIndex makeLuceneIndex(final File luceneDirectory) {
        return new AbstractLuceneIndex(luceneName, new PorterStemAnalyzer()) {

            public Directory getDirectory(String name, boolean create) throws IOException {
                return FSDirectory.getDirectory(
                    new File(luceneDirectory, name).toString(), create);
            }
        };
    }

    public LuceneFileSystemIndex(File indexDirectory) throws IOException, IndexVersionException {
        super(indexDirectory);
        this.erg = new ElementRelationshipGraph();
        final File luceneDirectory = new File(indexDirectory, "lucene");
        this.luceneIndex = makeLuceneIndex(luceneDirectory);
    }

    @Override
    public NEXISearcher getNEXISearcher() throws IOException {
        return new LuceneNEXISearcher(erg, this, luceneIndex);
    }

    public void open(final boolean create) throws IOException {
        writer = luceneIndex.getWriter(create);
    }
    
    public void close() throws IOException {
        try {
            writer.optimize();
        } finally {
            writer.close();
        }
    }

    @Override
    public WritableIndex getWritableIndex() throws IOException {
        return new LuceneWritableIndex(luceneIndex.getWriter(true));
    }

    @Override
    public String getIndexVersion() {
        return "lucene1";
    }

}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/